<?php

namespace app\admin\model\jeek;

use app\admin\model\jeek\shop\SkuGoods;
use app\common\model\User;
use tencent\api\Im;
use think\Model;


class Attestation extends Model
{


    // 表名
    protected $name = 'jeek_attestation';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_text'
    ];


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }

    public function getTypeList()
    {
        return ['wechat' => __('Type wechat'), 'alipay' => __('Type alipay')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function skuGoods()
    {
        return $this->belongsTo(SkuGoods::class, 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



    /**
     * Account:咨询师认证回调方法
     * User: Ixiangang
     * Date: 2025/1/18
     * Time: 13:34
     */
    public function notify($order_sn, $pay_price, $pay_sn = '', $paytype = '')
    {
        $jeekAttestation = self::get(['order_sn' => $order_sn]);
        if (!$jeekAttestation) return false;
        if ($jeekAttestation->status != 1) return false;
        $jeekAttestation->status = '2';
        $jeekAttestation->pay_price = $pay_price;
        $jeekAttestation->pay_time = time();
        if ($pay_sn) {
            $jeekAttestation->pay_sn = $pay_sn;
        }
        if ($paytype) {
            $jeekAttestation->pay_type = $paytype;
        }
        $jeekAttestation->save();
        # 更新咨询师表信息
        $goods = SkuGoods::where(['id' => $jeekAttestation->goods_id])->find();
        $goods->ispay = 1;
        $goods->payfees = bcadd($pay_price,$goods->payfees,2);
//        $goods->tencent_account = "consultant" . $jeekAttestation->goods_id;
//        # 咨询师绑定腾讯IM
//        $im = new Im();
//        $chk_add_im = $im->check_bind($goods->tencent_account);
//        if (empty($chk_add_im)) {
//            $im->account_import($goods->tencent_account, $goods->name, cdnurl($goods->image,true));
//        }
        $goods->save();
        return true;
    }


}
